﻿#include "LoginDialog.h"
#include "ui_LoginDialog.h"
#include <QDateTime>
#include <QMessageBox>
#include "Sqlite/DeviceDBManager.h"
LoginDialog::LoginDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::LoginDialog)
{
    ui->setupUi(this);

    //隐藏授权UI
    ui->fm_grantno->setVisible(false);
    //设置按钮可选中
    ui->btn_pwdlogin->setCheckable(true);
    ui->btn_fingerlogin->setCheckable(true);
    ui->btn_facelogin->setCheckable(true);
    ui->btn_pwdlogin->setChecked(true);

    //设置用户名为上一次登录用户
    ui->ip_username->setText(DeviceDBManager::getInstance()->getDeviceInfo("LastLoginUser").toString());
    //如果设置是保存密码，则填入用户密码
    if(DeviceDBManager::getInstance()->getDeviceInfo("RemenberPwd").toBool())
    {
        QString pwd;
        DeviceDBManager::getInstance()->getUserPwd(ui->ip_username->text(),pwd);
        ui->ip_pwd->setText(pwd);
    }
    //默认用户密码登录
    ui->ck_pwd->blockSignals(true);
    ui->ck_pwd->setChecked(DeviceDBManager::getInstance()->getDeviceInfo("RemenberPwd").toBool());
    ui->ck_pwd->blockSignals(false);

    //检查是否授权
    if(!DeviceDBManager::getInstance()->getDeviceInfo("GrantNo").toBool())
    {
        QString deploymentTime = DeviceDBManager::getInstance()->getDeviceInfo("SystemDesployTime").toString();
        //转换格式
        QDateTime startDate=QDateTime::fromString(deploymentTime, "yyyyMMddhhmmss");
        //获取系统当前时间
        QDateTime curSysTime = QDateTime::currentDateTime();
        //如果部署时间距当前时间大于60天则显示授权frame
        if(startDate.daysTo(curSysTime)<=60)
        {
            //显示授权UI
            ui->fm_grantno->setVisible(true);
            //延期次数大于2次，试用按钮失能
            postpoinetime = DeviceDBManager::getInstance()->getDeviceInfo("PostponeTime").toInt();
            if(postpoinetime>=2)
            {
                ui->btn_postpone->setEnabled(false);
                ui->lb_grantno->setText(QString("试用期时间已到，您已延期试用2次，共可延期2次，请联系厂家进行授权。"));
            }
            else
            {
                ui->lb_grantno->setText(QString("试用期时间已到，您已延期试用%1次，共可延期2次，一次60天。或直接联系厂家进行授权。").arg(postpoinetime));
            }
            //根据部署时间进行一次MD5加密然后取出加密后的数字作为序列号显示出来
            QByteArray data = deploymentTime.toUtf8();
            QByteArray hash = QCryptographicHash::hash(data, QCryptographicHash::Md5);
            QString serial = QString(hash.toHex());
            serial = getNumFromString(serial);
            ui->ip_serial->setText(serial);
        }
    }
}

LoginDialog::~LoginDialog()
{
    delete ui;
}


void LoginDialog::on_btn_pwdlogin_clicked()
{
    //显示密码登录页面
    ui->btn_facelogin->setChecked(false);
    ui->btn_fingerlogin->setChecked(false);

    ui->fm_pwdmain->setVisible(true);
    ui->fm_facemain->setVisible(false);
    ui->fm_fingermain->setVisible(false);
}

void LoginDialog::on_btn_fingerlogin_clicked()
{
    //显示指纹页面
    ui->btn_pwdlogin->setChecked(false);
    ui->btn_facelogin->setChecked(false);

    ui->fm_pwdmain->setVisible(false);
    ui->fm_facemain->setVisible(false);
    ui->fm_fingermain->setVisible(true);
}

void LoginDialog::on_btn_facelogin_clicked()
{
    //显示面部登录页面
    ui->btn_pwdlogin->setChecked(false);
    ui->btn_fingerlogin->setChecked(false);

    ui->fm_pwdmain->setVisible(false);
    ui->fm_facemain->setVisible(true);
    ui->fm_fingermain->setVisible(false);
}

void LoginDialog::on_ck_pwd_stateChanged(int arg1)
{
    DeviceDBManager::getInstance()->setDeviceInfo("RemenberPwd",QString::number(arg1));
}

void LoginDialog::on_btn_pwd_clicked()
{
    if(!DeviceDBManager::getInstance()->containsUserName(ui->ip_username->text()))
    {
        QMessageBox msgBox;
        msgBox.setText("用户名不存在！");
        msgBox.exec();
        return;
    }
    QString pwd;
    DeviceDBManager::getInstance()->getUserPwd(ui->ip_username->text(),pwd);
    if(ui->ip_pwd->text() != pwd)
    {
        QMessageBox msgBox;
        msgBox.setText("密码不正确！");
        msgBox.exec();
        return;
    }
    DeviceDBManager::getInstance()->setDeviceInfo("LastLoginUser",ui->ip_username->text());
    accept();
}

QString LoginDialog::getNumFromString(QString str)
{
    QRegularExpression re("[a-zA-Z]+");
    QString result = str;
    result.replace(re, "");
    if(result.size() >= 16)
        result = result.mid(4,12);
    return result;
}

void LoginDialog::on_btn_postpone_clicked()
{
    DeviceDBManager::getInstance()->setDeviceInfo("PostponeTime",QString::number(postpoinetime+1));
    DeviceDBManager::getInstance()->setDeviceInfo("SystemDesployTime",QDateTime::currentDateTime().toString("yyyyMMddhhmmss"));
    QMessageBox msgBox;
    msgBox.setText("延期试用60天成功！");
    msgBox.exec();
    ui->fm_grantno->setVisible(false);
}

void LoginDialog::on_btn_grant_clicked()
{
    if(ui->ip_grantno->text().isEmpty())
    {
        QMessageBox msgBox;
        msgBox.setText("请填写授权码！");
        msgBox.exec();
        return;
    }
    //根据序列号进行一次MD5加密，将加密后的数字取出来作为授权码
    QByteArray data = ui->ip_serial->text().toUtf8();
    QByteArray hash = QCryptographicHash::hash(data, QCryptographicHash::Md5);
    QString grantno = QString(hash.toHex());
    grantno = getNumFromString(grantno);
    if(grantno != ui->ip_grantno->text())
    {
        QMessageBox msgBox;
        msgBox.setText("授权码错误！");
        msgBox.exec();
        return;
    }
    DeviceDBManager::getInstance()->setDeviceInfo("GrantNo","1");   //TEXT类型非0即是true
    QMessageBox msgBox;
    msgBox.setText("授权成功！");
    msgBox.exec();
    ui->fm_grantno->setVisible(false);
}
